package com.ndpmedia.fb.pmd.configration.db;

import com.ndp.fb.util.DateUtil;
import com.ndpmedia.fb.pmd.configration.model.po.ConfigPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mikey.zhaopeng on 2014/10/12 20:47.
 * Email mikey.zhao@ndpmedia.com
 */
@Repository
public class ConfigRepository extends BasePepository {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    public ConfigPo findById(String configid){
        String sql = "select * from config where id='" + configid + "'";
        ConfigPo po = (ConfigPo) this.jdbcTemplate.queryForObject(sql,new ConfigRowMapper());
        return po;
    }

    public List<ConfigPo> findAll(String appid) {
        String sql = "select * from config where appid='" + appid + "'";
        return queryForListObject(sql, ConfigPo.class);
    }

    public ConfigPo save(final String appid, final String name) {

        final  String sql = "INSERT INTO config(APPID, NAME,UPDATE_TIME) VALUES(?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, appid);
                ps.setString(2, name);
                ps.setString(3, DateUtil.getCurrDate(DateUtil.FORMAT_ONE));
                return ps;
            }
        }, keyHolder);

        Integer id = keyHolder.getKey().intValue();

        ConfigPo config = new ConfigPo(id,appid,name);
        return config;
    }


    public boolean delete(String configid) {
        Map params = new HashMap();
        params.put("configid", configid);
        String sql = "delete from config where id=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, configid);
        return true;
    }

    public boolean deleteByAppId(String appid) {
        Map params = new HashMap();
        params.put("appid", appid);
        String sql = "delete from config where appid=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, appid);
        return true;
    }


    public boolean update(String configid, String name) {
        Map params = new HashMap();
        params.put("configid", configid);
        params.put("name", name);
        String sql = "update config set name=? where id=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, name, configid);
        ConfigPo config = new ConfigPo(Integer.parseInt(configid),"",name);
        return true;
    }

    public boolean updateStatus(String configid, String status) {
        Map params = new HashMap();
        params.put("configid", configid);
        params.put("status", status);
        String sql = "update config set status=? where id=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, status, configid);
        ConfigPo config = new ConfigPo(Integer.parseInt(configid),"","");
        return true;
    }

    class ConfigRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet rs, int value) throws SQLException {
            ConfigPo configPo = new ConfigPo();
            configPo.setId(rs.getInt("ID"));
            configPo.setAppId(rs.getString("APPID"));
            configPo.setName(rs.getString("NAME"));
            configPo.setDescription(rs.getString("DESCRIPTION"));
            configPo.setCreateTime(rs.getString("CREATE_TIME"));
            configPo.setUpdateTime(rs.getString("UPDATE_TIME"));
            configPo.setStatus(rs.getInt("STATUS"));

            return configPo;
        }
    }

}
